package org.ghost.wechat.platform.util.xml;

import javax.xml.stream.XMLStreamWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author 01
 */
public class CDataHandler implements InvocationHandler {
    private static final String METHOD_WRITE_CHARACTERS = "writeCharacters";

    private XMLStreamWriter writer;

    public CDataHandler(XMLStreamWriter writer) {
        this.writer = writer;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //1、writeCharacters(String text)
        //2、writeCharacters(char[] text, int start, int len)
        if (METHOD_WRITE_CHARACTERS.equals(method.getName())) {
            if (args.length == 1) {
                String text = (String) args[0];
                writer.writeCData(text);
                return null;
            } else if (args.length == 3) {
                char[] chs = (char[]) args[0];
                writer.writeCData(new String(chs));
                return null;
            }
        }
        return method.invoke(writer, args);
    }
}